package com.tegistuff.webos.run.debugger.commands;

import com.intellij.xdebugger.breakpoints.XBreakpointProperties;
import com.intellij.xdebugger.breakpoints.XLineBreakpoint;
import com.tegistuff.webos.run.debugger.breakpoints.WebOSBreakpointWrapper;
import org.json.JSONException;
import org.json.JSONObject;

/**
 * WebOSSetBreakpointCommand.java
 *
 * Copyright (C) 2011-2012 Timo Tegtmeier.
 * All rights reserved.
 */
public class WebOSSetBreakpointCommand extends WebOSDebuggerCommand {

    private XLineBreakpoint<XBreakpointProperties> myBreakpoint;
    private String myFileName;
    private int myLineNumber;

    public WebOSSetBreakpointCommand(XLineBreakpoint<XBreakpointProperties> breakpoint, String filename, int line) {
        myBreakpoint = breakpoint;
        myFileName = filename;
        myLineNumber = line;
    }

    @Override
    public void execute() throws JSONException {
        runRequest(ourAppDebugSession.getSetBreakpointRequest(myFileName, myLineNumber));
    }

    @Override
    protected void onSuccess() throws JSONException {
        JSONObject body = ourResponse.getJSONObject("body");
        int bpNumber = body.getInt("breakpoint");

        WebOSBreakpointWrapper bp = new WebOSBreakpointWrapper(myFileName, myLineNumber, bpNumber, myBreakpoint);
        ourDebugProcess.registerBreakpoint(bp);
    }
}
